<?php

namespace Apps\Common\Libs;


use Apps\Common\Models\SysAreaZipDistrictPhonecode;
use Apps\Common\Models\SysOpenCity;
use PhalconPlus\Common\Component;

/**
 * Class SysArea
 * @package Apps\Common\Libs
 */
class SysArea extends Component
{
    /**
     * 通过区县标识获取开通城市ID
     * @param $district
     * @return int
     */
    public static function districtToOpenCityId($district)
    {
        // 获取地区的区号
        $area = SysAreaZipDistrictPhonecode::findFirst('K_ID = ' . $district);
        if(!$area) {
            return 0;
        }
        $phoneCode = $area->PHONE_CODE;
        $openCityInfo = SysOpenCity::query()
            ->where('city_code = :code:', ['code' => $phoneCode])
            ->execute()
            ->getFirst();
        if(!$openCityInfo) {
            return 0;
        }
        return $openCityInfo->city_id;
    }
}